def view_tasks(todo_list):
    if not todo_list:
        print("Sorry,your to-do list is empty!")
        return

    print("\nHow would you like to sort your tasks?")
    print("1. By importance")
    print("2. By deadline(Earliest First)")

    while True:
        try:
            choice = int(input("Please enter your choice (1 or 2): "))
            if choice in [1, 2]:
                break
            else:
                print("Please enter 1 or 2")
        except ValueError:
            print("Please enter a valid number")

    #make sure the original to-do list wouldn't be changed by these algorithm
    sorted_tasks = todo_list.copy()

    if choice == 1:
        #sorted by importance
        choice2 = int(input("please choose high_to_low(1) or low_to_high(2) just number plz"))
        if choice2 == 1:
            sorted_tasks.sort(key=lambda x: x["importance"], reverse=True)
            print("\n=== Tasks Sorted by Importance (High to Low) ===")

        if choice2 ==2:
            sorted_tasks.sort(key=lambda x: x["importance"], reverse=False)
            print("\n=== Tasks Sorted by Importance (Low to High) ===")

    else:
        # sorted by ddl (the cloest date in the front
        sorted_tasks.sort(key=lambda x: x["DDL"])
        print("\n=== Tasks Sorted by Deadline (Earliest First) ===")

    #In order to let finished works in the bottom, divide the list into two
    finished_tasks = []
    unfinished_tasks = []

    for task in sorted_tasks:
        if task['finish']:
            finished_tasks.append(task)
        else:
            unfinished_tasks.append(task)

    final_list = unfinished_tasks + finished_tasks

    for i, task in enumerate(final_list, 1):
        status = "✓" if task["finish"] else "✗"
        importance_stars = "★" * task["importance"]
        daily_info = "Daily" if task["daily"] else f"DDL: {task['DDL']}"



        print(f"{i}. [{status}] {task['content']}")
        print(f"   Tag: {task['tag']} | Importance: {importance_stars} | {daily_info}")
        print()
    
def test_view_tasks():
    """简单的测试函数"""
    
    # 测试数据
    test_tasks = [
        {
            "content": "Complete Python project",
            "tag": "study",
            "finish": False,
            "importance": 3,
            "daily": False,
            "DDL": 20241025
        },
        {
            "content": "Morning jogging",
            "tag": "life",
            "finish": True,
            "importance": 2,
            "daily": True,
            "DDL": 20241020
        },
        {
            "content": "Team meeting",
            "tag": "work",
            "finish": False,
            "importance": 1,
            "daily": False,
            "DDL": 20241022
        }
    ]
    
    print("=" * 40)
    print("开始测试 view_tasks 函数")
    print("=" * 40)
    
    # 测试空列表
    print("测试1: 空列表")
    view_tasks([])
    
    print("=" * 40)
    print("测试2: 正常任务列表")
    print("=" * 40)
    
    # 调用函数进行测试
    view_tasks(test_tasks)
    
    print("=" * 40)
    print("测试完成!")
    print("=" * 40)

# 如果你想要更详细的测试，可以使用这个版本
def detailed_test_view_tasks():
    """更详细的测试函数"""
    
    # 更丰富的测试数据
    test_tasks = [
        {
            "content": "High importance task",
            "tag": "work",
            "finish": False,
            "importance": 3,
            "daily": False,
            "DDL": 20241030
        },
        {
            "content": "Low importance task",
            "tag": "life",
            "finish": False,
            "importance": 1,
            "daily": False,
            "DDL": 20241015
        },
        {
            "content": "Medium importance finished task",
            "tag": "study",
            "finish": True,
            "importance": 2,
            "daily": False,
            "DDL": 20241025
        },
        {
            "content": "Daily routine",
            "tag": "life",
            "finish": False,
            "importance": 2,
            "daily": True,
            "DDL": 20241020
        }
    ]
    
    print("详细测试 view_tasks 函数")
    print("测试数据包含:")
    print("- 4个任务")
    print("- 不同重要性级别 (1-3)")
    print("- 已完成和未完成任务")
    print("- 日常任务和普通任务")
    print()
    
    view_tasks(test_tasks)

# 运行测试
if __name__ == "__main__":
    # 选择测试模式
    print("选择测试模式:")
    print("1. 简单测试")
    print("2. 详细测试")
    
    try:
        choice = int(input("请输入 1 或 2: "))
        if choice == 1:
            test_view_tasks()
        elif choice == 2:
            detailed_test_view_tasks()
        else:
            print("无效输入，运行简单测试")
            test_view_tasks()
    except ValueError:
        print("输入错误，运行简单测试")
        test_view_tasks()